<?php

namespace app\common\auth;

use think\Session;

class Auth
{
    /**
     * 检查权限是否存在
     * @param string $controller
     * @param string $action
     * @return bool
     */
    public function check($controller = '', $action = '')
    {
        $session = new Session();
        $user = $session->get('currentUser');
        if (empty($user)) {
            return false;
        }
        if ($user['id'] == 1) { //如果为系统超级管理员则不受限制
            return true;
        }
        if (empty($controller) || empty($action)) {
            return false;
        }
        if (strtolower($controller) == "index" && $action == 'index') { //首页不需要权限判断
            return true;
        }
        $urlArr = $session->get('urlArr');
        $url = strtolower($controller) . '/' . $action;
        if (!in_array($url, $urlArr)) {
            return false;
        }
        return true;
    }

    /**
     * 检查前台权限是否存在
     * @param string $controller
     * @param string $action
     * @return bool
     */
    public function memberCheck($controller = '', $action = '')
    {
        $session = new Session();
        $member = $session->get('currentMember');
        if (empty($member)) {
            return false;
        }
        if ($member['id'] == 1) { //如果为系统超级管理员则不受限制
            return true;
        }
        if (empty($controller) || empty($action)) {
            return false;
        }
        if (strtolower($controller) == "index" && $action == 'index') { //首页不需要权限判断
            return true;
        }
        $urlArr = $session->get('memberUrlArr');
        $url = strtolower($controller) . '/' . $action;
        if (!in_array($url, $urlArr)) {
            return false;
        }
        return true;
    }
}